/**
 * 
 */
package jnzb.ui;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;

import jnzb.JNZB;
import jnzb.model.event.EventTableModel;
import jnzb.ui.event.EventFrame;
import jnzb.ui.event.EventPanel;

/**
 * Shows the latest info message plus a button to show all in another dialog
 * 
 * @author Rid
 * 
 */
@SuppressWarnings("serial")
public class InfoPanel extends JPanel {
	private JLabel infoField = new JLabel("Sitting here waiting!");
	private JButton showAll = new JButton("Show All");
	private EventTableModel infoModel = null;
	private EventFrame eventFrame = null;

	public InfoPanel() {
		create();
		registerListeners();
	}

	private void create() {
		Border etchedBorder = BorderFactory
				.createEtchedBorder(EtchedBorder.LOWERED);
		Border titleBorder = BorderFactory.createTitledBorder(etchedBorder,
				"Information");
		this.setBorder(titleBorder);

		this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

		Font boldFont = new Font(super.getFont().getName(), Font.BOLD, super
				.getFont().getSize());

		JLabel info = new JLabel("Info");
		info.setFont(boldFont);
		info.setMaximumSize(info.getPreferredSize());

		infoField.setHorizontalTextPosition(JLabel.LEFT);
		infoField.setHorizontalAlignment(JLabel.LEFT);

		showAll.setMaximumSize(showAll.getPreferredSize());

		this.add(Box.createHorizontalStrut(10));
		this.add(info);
		this.add(Box.createHorizontalStrut(5));
		this.add(infoField);
		this.add(Box.createHorizontalGlue());
		this.add(showAll);
		this.add(Box.createHorizontalStrut(10));

		int h = this.getPreferredSize().height;

		// Set max size to the preferred size so it'll only resize horizontally
		this.setMaximumSize(new Dimension(9999, h));

		eventFrame = new EventFrame();

		updateInfo();

	}

	public void setInfoModel(EventTableModel eventModel) {
		this.infoModel = eventModel;
		this.eventFrame.getEventPanel().setModel(eventModel);
	}

	/**
	 * Temporarily stores the current info value locally
	 */
	public String getDisplayedText() {
		return infoField.getText();
	}


	/**
	 * Update the info display with the latest value in the model.<br>
	 */
	public void updateInfo() {
		// Get the latest at the time of method executiom
		final String latestInfo;
		if (infoModel != null && !infoModel.isEmpty())
			latestInfo = infoModel.getFirst();
		else
			latestInfo = "jNZB Version " + JNZB.VERSION;

		Runnable r = new Runnable() {
			public void run() {
				infoField.setText(latestInfo);
				infoField.setToolTipText(latestInfo);
			}
		};
		SwingUtilities.invokeLater(r);
	}

	public void registerListeners() {
		this.showAll.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent event) {
				eventFrame.setVisible(true);
			}
		});

	}

	public EventPanel getEventPanel() {
		return this.eventFrame.getEventPanel();
	}
	
	/**
	 * Hide the event frame
	 */
	public void hideEventFrame(){
		this.eventFrame.setVisible(false);
	}
}
